
package gui.event.tiemchung;

import com.toedter.calendar.JDateChooser;
import control.tiemchung.TCMainPanel_ctrl;
import gui.event.khachhang.KHMainPanel_evt;
import gui.event.ulti.Util;
import gui.tiemchung.TCChiTiet_Dialog;
import gui.tiemchung.TCSuaHoSo_Dialog;
import gui.tiemchung.TCThemHoSo_Full;
import gui.tiemchung.TCThemPhieuTiemChung_Full;
import gui.util.Confirm_dialog;
import java.awt.print.PrinterException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import model.dao.phieutiemchung.PhieuTiemChungDAO;
import model.entity.HoSoTiemChung;
import model.entity.PhieuDieuTri;
import model.entity.PhieuTiemChung;
import model.entity.VatNuoi;

public class TCMainPanel_evt {
    public static final void btnThemActionPerformed(JTable table){
        
        TCThemHoSo_Full tcThemFull = new TCThemHoSo_Full(table,null, true);
        
        tcThemFull.setLocationRelativeTo(null);
        tcThemFull.setVisible(true);
    }
    
    public static final void btnSuaActionPerformed(JTable table) {
        HoSoTiemChung hoSoTiemChung = TCMainPanel_ctrl.chiTietHoSo(table);
        int maHoSo = hoSoTiemChung.getHstcMa();
        String benh = hoSoTiemChung.getBenh();
        Date ngayBatDau = hoSoTiemChung.getHstcNgayBd();
        Date ngayKetThuc = hoSoTiemChung.getHstcNgayKt();
        String trangThai = hoSoTiemChung.getHstcTrangThai();
        String ghiChu = hoSoTiemChung.getHstcGhiChu();
        int maVatNuoi = 0;
        VatNuoi vatNuoi = hoSoTiemChung.getVatNuoi();
        
        TCSuaHoSo_Dialog tcSuaDlg = new TCSuaHoSo_Dialog(table, null, true);
        if(vatNuoi.getVnMa().equals("")){
        }else{
            maVatNuoi = vatNuoi.getVnMa();
        }
        
        tcSuaDlg.getTxtMaHSTiemChung().setText(maHoSo + "");
        tcSuaDlg.getTxtTenBenh().setText(benh);
        tcSuaDlg.getTxtMaVatNuoi().setText(maVatNuoi + "");
        tcSuaDlg.getjDateChooserNgayBatDau().setDate(ngayBatDau);
        tcSuaDlg.getjDateChooserNgayKetThuc().setDate(ngayKetThuc);
        tcSuaDlg.getTxtTrangThai().setText(trangThai);
        tcSuaDlg.getjTextAreaChuThich().setText(ghiChu);
        
        tcSuaDlg.setLocationRelativeTo(null);
        tcSuaDlg.setVisible(true);
    }
    public static final void btnChiTietActionPerformed(JTable table) {
        HoSoTiemChung hoSoTiemChung = TCMainPanel_ctrl.chiTietHoSo(table);
        int maHoSo = hoSoTiemChung.getHstcMa();
        String benh = hoSoTiemChung.getBenh();
        Date ngayBatDau = hoSoTiemChung.getHstcNgayBd();
        Date ngayKetThuc = hoSoTiemChung.getHstcNgayKt();
        String trangThai = hoSoTiemChung.getHstcTrangThai();
        String ghiChu = hoSoTiemChung.getHstcGhiChu();
        
        VatNuoi vatNuoi = hoSoTiemChung.getVatNuoi();
        System.out.println(vatNuoi);
        
        TCChiTiet_Dialog tcChiTietDlg = new TCChiTiet_Dialog(null, true);
        
        if(vatNuoi.getVnMa().equals("")){
        }else{
            int maVatNuoi = vatNuoi.getVnMa();
            String tenVatNuoi = vatNuoi.getVnTen();
            tcChiTietDlg.getTxtTenVatNuoi().setText(tenVatNuoi);
        }
        
        tcChiTietDlg.getTxtMaTC().setText(maHoSo + "");
        tcChiTietDlg.getTxtBenh().setText(benh);
       
        tcChiTietDlg.getjDateChooserNgayBatDau().setDate(ngayBatDau);
        tcChiTietDlg.getjDateChooserNgayKetThuc().setDate(ngayKetThuc);
        tcChiTietDlg.getTxtTrangThai().setText(trangThai);
        
        List<PhieuTiemChung> list = PhieuTiemChungDAO.timPTCTheoMaHoSo(maHoSo);
        TCMainPanel_ctrl.loadListPTCLenTable(list, tcChiTietDlg.getTblTiemChung());
        
        tcChiTietDlg.setLocationRelativeTo(null);
        tcChiTietDlg.setVisible(true);
        
    }
    public static final void btnCloseActionPerformed(JPanel pnl, JTabbedPane jTabbedPane) {
        Util.closeATab(pnl.getName(), jTabbedPane);
    }

    public static final void btnLietKeActionPerformed(JTable table, JTextField txtMaHoSo, JTextField txtMaVatNuoi, JTextField txtTenBenh, JTextField txtTenVatNuoi, JDateChooser jDateChooserNgayBatDau, JDateChooser jDateChooserNgayKetThuc, ButtonGroup buttonGroupTrangThai) {
//        TCMainPanel_ctrl.loadAllHoSoTiemChung(table);
        String maHoSo = txtMaHoSo.getText();
        String maVatNuoi = txtMaVatNuoi.getText();
        String tenBenh = txtTenBenh.getText();
        String tenVatNuoi = txtTenVatNuoi.getText();
        Date ngayBatDau = jDateChooserNgayBatDau.getDate();
        Date ngayKetThuc = jDateChooserNgayKetThuc.getDate();
        String trangThai = "";
        
        if(buttonGroupTrangThai.getSelection() != null)
            trangThai = buttonGroupTrangThai.getSelection().getActionCommand();
        if(maHoSo.equals("") && maVatNuoi.equals("") && tenBenh.equals("") && tenVatNuoi.equals("") && ngayBatDau == null && ngayKetThuc == null && trangThai.equals(""))
            TCMainPanel_ctrl.loadAllHoSoTiemChung(table);
        try{
            if(!maHoSo.equalsIgnoreCase(""))
                TCMainPanel_ctrl.timHoSoTiemChungTheoMa(Integer.parseInt(maHoSo), table);
            else{
                if(!maVatNuoi.equalsIgnoreCase(""))
                    TCMainPanel_ctrl.timHoSoTiemChungTheoMaVatNuoi(Integer.parseInt(maVatNuoi), table);
            }
            
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
            
        }
        
    }
    public static void btnInActionPerformed(JTable table) {
        try {
            table.print();
        } catch (PrinterException ex) {
            Logger.getLogger(KHMainPanel_evt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void btlTiemChungFocusGained(List<JButton> listButton, JTable table) {
        if (table.getSelectedRowCount() == 1) 
            Util.enableListButtons(listButton);
        
    }

    public static void btnThemPhieuTiemChungActionPerformed() {
        JDialog tcPhieuDieuTri = new TCThemPhieuTiemChung_Full(null, true);
        tcPhieuDieuTri.setLocationRelativeTo(null);
        tcPhieuDieuTri.setVisible(true);
    }

    public static void btnXoaActionPerformed(JTable btlTiemChung) {
        Confirm_dialog confirm = new Confirm_dialog(null, true);
        if(confirm.isResult() == true){
            int row = btlTiemChung.getSelectedRow();
            try{
                System.out.println(btlTiemChung.getValueAt(row, 0));
                int maHoSo = (Integer) btlTiemChung.getValueAt(row, 0);
            
                if(TCMainPanel_ctrl.xoaHoSoTiemChung(maHoSo) == true ){
                    JOptionPane.showMessageDialog(null, "Đã xóa !");
                   resetKeyboardActionsTiemChung(btlTiemChung);
                }else{
                    JOptionPane.showMessageDialog(null, "Không xóa được !");
                }
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, "Không xóa được ...! /n " + e.getMessage());
            }
        }
    }
    public static void resetKeyboardActionsTiemChung(JTable table){
        TCMainPanel_ctrl.loadAllHoSoTiemChung(table);
    }
}
